<!--
* description: 整车销售---开票审核
* author:xlc
* createdDate: 2020-11-26
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-show-more-btn="true"
      :callback-obj="callbackObj"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import { orgApis } from '@/api/vesalesapi.js'
import OneTableTemplate from '@/components/templates/oneTable'
import Vue from 'vue'
export default {
  name: 'Orderaudit',
  components: {
    OneTableTemplate
  },
  mixins: [oneTableWithViewTemplateMixins],
  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, 'supplierMessage-edit'], () => {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      titleName: '',
      editRowData: {},
      editPopupsState: '',
      editPopupsVisible: false,
      // 网格查询API配置对象
      apiConfig: orgApis.orderauditQuery,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          name: 'search', // 按钮图标样式
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey4',
          type: '',
          size: 'small',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          name: 'reset',
          position: 'right' // 重置
        },
        {
          compKey: 'btnKey5',
          type: '',
          size: 'small',
          disabled: true,
          parentFileds: 'disabled:table',
          clickEvent: () => this.pass('1'),
          text: '审核通过', // 新增
          name: 'adopt',
          position: 'left'
        },
        {
          compKey: 'btnKey5',
          disabled: true,
          parentFileds: 'disabled:table',
          type: '',
          size: 'small',
          clickEvent: () => this.pass('0'),
          text: '驳回', // err
          name: 'reject',
          position: 'left'
        }
      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 显示组件
            {
              compKey: 'compKey1',
              labelName: '订单编号：',
              codeField: 'saleOrderCode',
              isMul: false,
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '客户姓名：',
              codeField: 'custName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '客户电话：',
              codeField: 'custTel',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '发票状态：',
              codeField: 'invoiceApplyStatusName',
              lookuptype: 'VE1132',
              component: () => import('@/components/org/LookupValue'),
              type: 'dropdownList',
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: 'VIN码：',
              codeField: 'vin',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: false
            },
            {
              compKey: 'compKey6',
              labelName: '交付门店：',
              codeField: 'deliveryDlrId',
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMust: false,
              oFields: 'deliveryDlrId,deliveryDlrShortName',
              customObj: {
                api: orgApis.ListOfDeliveryStore
              }
            },
            {
              compKey: 'compKey7',
              labelName: '交付专员：',
              codeField: 'empId',
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMust: false,
              parentFileds: 'value:deliveryDlrId',
              oFields: 'empId,empName',
              customObj: {
                listQuery: { deliveryDlrId: '' },
                api: orgApis.ListOfdeliveryPersonnel
              }
            },
            {
              compKey: 'compKey9',
              labelName: '合同签署人：',
              codeField: 'signatoryName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey10',
              labelName: '合同签署人电话：',
              codeField: 'signatoryPhone',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey11',
              labelName: '开票日期：',
              codeField: 'invoiceDateStartTime,invoiceDateEndTime',
              component: () => import('@/components/org/datePicker/datePicker.vue'),
              dateType: 'daterange',
              type: 'inputText',
              isMul: false,
              isMust: false
            }
          ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'saleOrderCode',
              label: '订单编号', width: 120
            },
            {
              prop: 'orderStatusName',
              label: '订单状态', width: 120
            },
            {
              prop: 'invoiceApplyStatusName',
              label: '发票状态', width: 120
            },
            {
              prop: 'dlrShortName',
              label: '交付门店', width: 120
            },
            {
              prop: 'empName',
              label: '交付专员', width: 120
            },
            {
              prop: 'signatoryName',
              label: '合同签署人', width: 120
            },
            {
              prop: 'signatoryIdCard',
              label: '证件号', width: 120
            },
            {
              prop: 'signatoryPhone',
              label: '合同签署人电话', width: 120
            },
            {
              prop: 'vin',
              label: 'VIN码', width: 160
            },
            {
              prop: 'carSeriesCn',
              label: '车系', width: 120
            },
            {
              prop: 'carConfigCn',
              label: '基础车型名称', width: 120
            },
            {
              prop: 'optionalPagName',
              label: '选装包', width: 120
            },
            {
              prop: 'carColorName',
              label: '车身颜色', width: 120
            },
            {
              prop: 'carIncolorName',
              label: '内饰颜色', width: 120
            },
            {
              prop: 'invoiceNo',
              label: '发票号码', width: 120
            },
            {
              prop: 'invoiceAmout',
              label: '税价合计', width: 120
            },
            {
              prop: 'invoiceDate',
              label: '开票日期', width: 180
            },
            {
              prop: 'invoiceEmpName',
              label: '开票人'
            }
          ],
      formField: {
        saleOrderCode: '',
        custName: '',
        custTel: '',
        invoiceApplyStatusName: '',
        vin: '',
        deliveryDlrId: '',
        empId: '',
        erpBookStatusName: '',
        signatoryName: '',
        signatoryPhone: '',
        invoiceDateStartTime: '',
        invoiceDateEndTime: ''
      },
      // 是否使用刷新Key的方式刷新弹窗
      resetDialogKey: false,
      selectList: [],
      // 单表格模版页面组件回调事件
      callbackObj: {
        // 当选择项发生变化时会触发该事件（多选时）
        onCheckSelectChange: (selection) => {
          this.selectList[0] = selection.map(t => t.saleOrderId)
          this.selectList[1] = selection.map(t => t.updateControlId)
          this.selectList[2] = selection.map(t => t.invApplyMId)
          this.selectList[3] = selection.map(t => t.updateControlIdM)
          this.onCheckSelectChange1(selection)
        }
      }
    }
  },
  mounted() {
    this.queryTable(1)
  },
  methods: {
    // 选择列是否生效
    getSelectable(row, index) {
      return !(row.orderStatusName === '开票审核通过')
    },
    // 审核
    pass(type) {
      this.$refs.multipleTable.listLoading = true
      const that = this
      var api
      if (type === '1') {
        api = orgApis.orderauditConfirm
      } else {
        api = orgApis.orderauditReject
      }
      const queryObj = {
        // api配置
        apiConfig: api,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          pageSize: '',
          pageIndex: '',
          // 当前中台使用的名称有dataInfo、info，具体查看API文档
          dataInfo: { saleOrderIds: this.selectList[0], updateControlIds: this.selectList[1], invApplyMIds: this.selectList[2], updateControlIdMs: this.selectList[3] }
        }
      }
      // 转换了中台请求格式数据
      var paramD = that.$getParams(queryObj)
      // 调用中台API方法（可复用）
      that.$requestAPI(paramD).then(response => {
        this.$message({
          message: response.msg,
          duration: 2000
        })
        if (response.result === '1') {
          this.$refs.multipleTable.listLoading = false
          this.queryTable()
        }
      })
    },
    onCheckSelectChange1(selection) {
      var btn1 = this.tableButtons[2]
      var btn2 = this.tableButtons[3]
      const isCheck = selection && selection.length > 0
      btn1.disabled = !isCheck
      btn2.disabled = !isCheck
    }
  }
}
</script>
